﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainigWorkshop.Service;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using TrainigWorkshop.Model;
using Presentation.ViewModel;
using TrainigWorkshop.Contract;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel;




namespace Presentation
{
    public partial class StudentMainPage : System.Web.UI.Page
    {
        public void Page_Load(object sender, EventArgs e)
        {
          if(!IsPostBack)
          {
              InitStudentInfo();
          }
        }

        private void InitStudentInfo()
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                List<StudentModel> student = channel.GetAllStudentInfo();
                List<StudentCourseDetailView> studentCourseDetailViewList = new List<StudentCourseDetailView>();
                StudentCourseDetailView studentCourseDetailView;
                foreach (StudentModel obj in student)
                {
                    studentCourseDetailView = new StudentCourseDetailView();
                    studentCourseDetailView.ClassName = obj.grades.ClassName;
                    studentCourseDetailView.StudentGender = obj.StudentGender;
                    studentCourseDetailView.StudentId = obj.StudentId;
                    studentCourseDetailView.StudentName = obj.StudentName;
                    studentCourseDetailViewList.Add(studentCourseDetailView);
                }
                gvStudentInfo.DataSource = studentCourseDetailViewList;
                gvStudentInfo.DataBind();
            }
        }

        protected void btnaddStudent_Click(object sender, EventArgs e)
        {
            Boolean type = true;
            Response.Redirect("UpdateStudent.aspx?type=" + type);
        }

        protected void gvStudentInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int StudentId = Convert.ToInt32(gvStudentInfo.DataKeys[e.RowIndex].Value);

            DeleteStudentInfo(StudentId);

        }

        private void DeleteStudentInfo(int StudentId)
        {
            using (ChannelFactory<IProjectBService> channelFactory = new ChannelFactory<IProjectBService>("ProjectBClient"))
            {
                IProjectBService channel = channelFactory.CreateChannel();
                channel.DeleteStudentInfo(StudentId);
                List<StudentModel> students = channel.GetAllStudentInfo();
                List<StudentCourseDetailView> studentCourseDetailViewList = new List<StudentCourseDetailView>();
                StudentCourseDetailView studentCourseDetailView;
                foreach (StudentModel obj in students)
                {
                    studentCourseDetailView = new StudentCourseDetailView();
                    studentCourseDetailView.ClassName = obj.grades.ClassName;
                    studentCourseDetailView.StudentGender = obj.StudentGender;
                    studentCourseDetailView.StudentId = obj.StudentId;
                    studentCourseDetailView.StudentName = obj.StudentName;
                    studentCourseDetailViewList.Add(studentCourseDetailView);
                }
                this.gvStudentInfo.DataSource = studentCourseDetailViewList;
                this.gvStudentInfo.DataKeyNames = new string[] { "StudentId" };
                this.gvStudentInfo.DataBind();
            }
        }

        protected void gvStudentInfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string studentId = this.gvStudentInfo.DataKeys[e.NewEditIndex].Value.ToString();
             Boolean type = false;
            Response.Redirect("UpdateStudent.aspx?id=" + studentId + "&type=" + type);
        }
    }
}